const { Controller } = require('egg');
const mongodb = require('../utils/mongo.js');
// const mongodb = require('mongodb');

class HomeController extends Controller {
  async getTemplate() {
    const { ctx } = this;
    // const { MongoClient } = mongodb;
    // const url = 'mongodb://localhost:27017/imooc-cli';
    // const client = new MongoClient(url);
    // const dbName = 'imooc-cli';
    // async function main() {
    //   // Use connect method to connect to the server
    //   const resConnect = await client.connect();
    //   const db = client.db(dbName);
    //   const collection = db.collection('project');
    //   const res111 = await collection.find({}).toArray();
    //   console.log(res111, 'res111');
    //   // collection.find({}, { projection: { _id: 0 } }).toArray((err, docs) => {
    //   //   if (err) {
    //   //     console.log(err);
    //   //   } else {
    //   //     console.log(docs, 'docsdocsdocs');
    //   //   }
    //   // });
    //   // the following code examples can be pasted here...

    //   return 'done.';
    // }
    // main()
    //   .then(res => {
    //   // console.log(res, 'jjjjj');
    //   })
    //   .catch(console.error)
    //   .finally(() => client.close());

    try {

      const data = await mongodb().query('project');
      ctx.body = data;
      // console.log('data');
    } catch (e) {
      // console.log(e.message);
    }
  }
}

module.exports = HomeController;
